{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/nceder/qpb4e/blob/main/code/Chapter%2006/Chapter_06.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "# 6 Strings\n",
        "\n",
        "# 6.1 Strings as sequences of characters"
      ],
      "metadata": {
        "id": "JZ14_-OadAV0"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "_aibBBweqVME",
        "outputId": "41f399a7-11df-45f3-b20f-226fc182fcec"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'H'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 1
        }
      ],
      "source": [
        "x = \"Hello\"\n",
        "x[0]"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[-1]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "w5mS2NSvq7Uf",
        "outputId": "33dcfd96-ca7f-4810-9385-708e04b45388"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'o'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[1:]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "LOSO2jCqq_So",
        "outputId": "030b4867-1ebf-4fc9-e2ff-f86fe74ee5ea"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'ello'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Goodbye\\n\"\n",
        "x = x[:-1]\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "17FujXUZrBas",
        "outputId": "96e9af71-0e75-4dc4-801f-fa49cf534038"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Goodbye'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "len(\"Goodbye\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IJwPv7MRrS9m",
        "outputId": "e6038eaf-3303-4dbd-c4ea-7bd57f1e41d8"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "7"
            ]
          },
          "metadata": {},
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.2 Basic string operations"
      ],
      "metadata": {
        "id": "VC3_h5M2dLWB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Hello \" + \"World\"\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "0dBuHywjrff4",
        "outputId": "72f25abf-7916-4ca3-a388-5b5c197a433d"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Hello World'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "8 * \"x\""
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "psICcGk_rwUq",
        "outputId": "8382f517-fcdd-49db-eafc-e768ab2a0e09"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'xxxxxxxx'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.3 Special characters and escape sequences\n",
        "\n",
        "## 6.3.2 Numeric (octal and hexadecimal) and Unicode escape sequences"
      ],
      "metadata": {
        "id": "R9mgP3LfdS06"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "'m'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "QVSBx1cWrymA",
        "outputId": "4fc044da-ada4-48a5-f303-96ea1e2ba297"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'m'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "'\\155'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "T-zwj6aFsCcm",
        "outputId": "37ace19c-1214-4a4e-865a-e5c70e370ca9"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'m'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "'\\x6D'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "TIDKGCJ_sGVI",
        "outputId": "1b1cdf65-819b-49e1-a62c-84c94b2e3e00"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'m'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "'\\n'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "DmRCu9QesJGU",
        "outputId": "93cf1c51-6a07-4ba2-8810-d24efee386c2"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'\\n'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "'\\012'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "CepidakCssdu",
        "outputId": "6c319ced-1e81-4925-c5e1-f5535857275a"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'\\n'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "'\\x0A'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "46wHu8KwsuP_",
        "outputId": "00cd6f5f-19a0-47a6-abe1-a386515f162f"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'\\n'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "unicode_a ='\\N{LATIN SMALL LETTER A}'           #A\n",
        "unicode_a"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "CFx7BVO1swAw",
        "outputId": "b0e8bb48-562b-4f17-a306-7aadd096e20e"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'a'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "unicode_a_with_acute = '\\N{LATIN SMALL LETTER A WITH ACUTE}'\n",
        "unicode_a_with_acute"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "HcZlFRxmsyR6",
        "outputId": "94132a73-b0e9-406f-fb79-583c0a176316"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'á'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"\\u00E1\"                    #B"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "QO3mlmGrs0SY",
        "outputId": "71712ab7-c669-445c-864a-56b4207b35e0"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'á'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.3.3 Printing vs. evaluating strings with special characters"
      ],
      "metadata": {
        "id": "MdA67diedkCC"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "'a\\n\\tb'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "M_fMG4VSs3K6",
        "outputId": "12e0c6f5-1069-4b9f-aa19-90a41ca2803b"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'a\\n\\tb'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print('a\\n\\tb')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jIdeqvZNtQyi",
        "outputId": "cf51686e-6662-4b84-fa31-93e0e9fa08e6"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "a\n",
            "\tb\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"abc\\n\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h5QGUltptSns",
        "outputId": "2b9b715f-fd27-4a46-afb3-85bd2248fc8e"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "abc\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"abc\\n\", end=\"\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TGnVM2VPtqcY",
        "outputId": "5486866e-3c3c-45a4-ff07-230504b834e1"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "abc\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.4 String methods\n",
        "\n",
        "## 6.4.1 The split and join string methods\n"
      ],
      "metadata": {
        "id": "A9gUufxpduyx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\" \".join([\"join\", \"puts\", \"spaces\", \"between\", \"elements\"])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "c3MthxkNtt3E",
        "outputId": "e4bdc02d-034c-4c6d-bf6a-2211b77ceadd"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'join puts spaces between elements'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"::\".join([\"Separated\", \"with\", \"colons\"])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "IbTxd_9jt9Ow",
        "outputId": "614ffb9d-2eb0-4ef9-eda0-f6023e65ab79"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Separated::with::colons'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"\".join([\"Separated\", \"by\", \"nothing\"])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "SMdzeGcTuIYE",
        "outputId": "591b761b-41e8-4d39-f1ef-fe560f666710"
      },
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Separatedbynothing'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"You\\t\\t can have tabs\\t\\n \\t and newlines \\n\\n \" \\\n",
        "               \"mixed in\"\n",
        "x.split()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MKyZHACRuYrh",
        "outputId": "b56c00b8-d473-45f2-d0bb-78b99d3bde22"
      },
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['You', 'can', 'have', 'tabs', 'and', 'newlines', 'mixed', 'in']"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.split(\"ss\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kzZkGOm_u340",
        "outputId": "c39dd87f-9294-426a-a5e7-5d3ad6353782"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['Mi', 'i', 'ippi']"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = 'a b c d'\n",
        "x.split(' ', 1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JmNrxm9lu8mc",
        "outputId": "9d58ab56-a6ed-47da-e2e2-45efb341e117"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['a', 'b c d']"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.split(' ', 2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gaC1u_o-vQXX",
        "outputId": "a382215d-40fc-425a-c6c2-91e9227270cb"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['a', 'b', 'c d']"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.split(' ', 9)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "U3U-NkKJvSbQ",
        "outputId": "46cce61e-f81b-4c42-dcdf-03e358ed72af"
      },
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['a', 'b', 'c', 'd']"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: split and join\n",
        "How could you use split and join to change all the whitespace in string x to dashes, such as changing \"this is a test\" to \"this-is-a-test\"?\n"
      ],
      "metadata": {
        "id": "LCLXZ2TTgJLu"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "CfG5QiDUgRAi"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"this is a test\"\n",
        "\"-\".join(x.split())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "qdRm0d-kgR-m",
        "outputId": "c147b568-efc4-49a3-ea5d-5fe507b10de2"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'this-is-a-test'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 1
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.4.2 Converting strings to numbers"
      ],
      "metadata": {
        "id": "ef3xjxfgd49K"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "float('123.456')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "85WO9R3kvXV5",
        "outputId": "bde9cd79-7c8b-4649-8340-8fd3140e3bcd"
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "123.456"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "float('xxyy')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "TjzwKwAMvxmY",
        "outputId": "3e5dd962-df73-477b-8890-69ad5952a842"
      },
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "could not convert string to float: 'xxyy'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-30-8caa265ffc7f>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'xxyy'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'xxyy'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "int('3333')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "joXaUc69v0UA",
        "outputId": "4feccd3b-4753-40d0-dbfb-4ae25357934b"
      },
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3333"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "int('123.456')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "bWXJdLjvv8QW",
        "outputId": "f616e7e6-47fc-47d1-ca67-0888266b2fb2"
      },
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "invalid literal for int() with base 10: '123.456'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-32-ed4c46a302ea>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'123.456'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '123.456'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "int('10000', 8)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QsstpvehwBIO",
        "outputId": "4d8c8d47-d3cb-428d-cb5c-b8eb2bfa4782"
      },
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "4096"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "int('101', 2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "k15vgBOawIiM",
        "outputId": "e661348e-9f14-46cd-e543-3cb107090f3b"
      },
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "5"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "int('ff', 16)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "A9nf-KX0wKlA",
        "outputId": "fbc8b6fb-07e0-427f-d78f-7680ffc275c2"
      },
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "255"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "int('123456', 6)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "yElCj3y9wNQ4",
        "outputId": "2d11e162-c897-4341-ffbc-936afa933b8f"
      },
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "invalid literal for int() with base 6: '123456'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-36-bdc1281d81c5>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'123456'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 6: '123456'"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Strings to Numbers\n",
        "Which of the following will not be converted to numbers, and why?\n",
        "```\n",
        "int('a1')\n",
        "int('12G', 16)\n",
        "float(\"12345678901234567890\")\n",
        "int(\"12*2\")\n",
        "```"
      ],
      "metadata": {
        "id": "u5daARJZgZ1E"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.4.3 Getting rid of extra whitespace"
      ],
      "metadata": {
        "id": "YC0TEMB7d-Xp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"  Hello,    World\\t\\t \"\n",
        "x.strip()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "iqm-z5Y9wPPD",
        "outputId": "90f77b9d-0f87-44f1-9379-d1ac90f66604"
      },
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Hello,    World'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.lstrip()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "HdKqfmDAxqtA",
        "outputId": "4cad19b5-a50b-49f3-8bcf-a7f12d9e0f81"
      },
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Hello,    World\\t\\t '"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.rstrip()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "zn61kBUxxs7G",
        "outputId": "c7246664-115d-404a-e564-7d2f05339086"
      },
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'  Hello,    World'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import string\n",
        "string.whitespace"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "0A5BOdi1xv44",
        "outputId": "0b09bd88-74a6-4b8c-ce45-e092ee3a02f9"
      },
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "' \\t\\n\\r\\x0b\\x0c'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\" \\t\\n\\r\\v\\f\""
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "gXyXmVNsx8DQ",
        "outputId": "55683681-8d73-4568-96e9-b522761fc31f"
      },
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "' \\t\\n\\r\\x0b\\x0c'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"www.python.org\"\n",
        "x.strip(\"w\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "p4XWRA0Lx_Dk",
        "outputId": "c829540c-ce87-46dc-f969-3b4ac8000ad4"
      },
      "execution_count": 42,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'.python.org'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.strip(\"gor\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "GesTZs0ByLXC",
        "outputId": "65a4be82-0e3a-4b89-8c9b-e0bdcc16147f"
      },
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'www.python.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.strip(\".gorw\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "FtyBhbziyPE0",
        "outputId": "36f2cb39-d6de-4c10-b5df-6fac46761597"
      },
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'python'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"www.python.org\"\n",
        "x.removeprefix(\"www.\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "YQMJKd0NyRvS",
        "outputId": "33612b13-0e9a-4752-e85a-f5599baa2376"
      },
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'python.org'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 45
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.removesuffix(\".org\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "rmDrQZg4yhCB",
        "outputId": "8a5669fb-e04b-41f7-e792-872d5f5fd7f2"
      },
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'www.python'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: strip\n",
        "If the string x equals \"(name, date),\\n\", which of the following returns a string containing \"name, date\" ?\n",
        "```\n",
        "1. x.rstrip(\"),\")\n",
        "2. x.strip(\"),\\n\")\n",
        "3. x.strip(\"\\n)(,\")\n",
        "4. x.strip(\"\\n)(,\") will remove the newline as well as the comma and parentheses.\n",
        "```"
      ],
      "metadata": {
        "id": "RRqFg_SMgj4R"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.4.4 String searching"
      ],
      "metadata": {
        "id": "Hr8ceI80eGp-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.find(\"ss\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MEdsdoMEyjvA",
        "outputId": "864eaf52-cab8-4683-a287-2884398c1fa2"
      },
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {},
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.find(\"zz\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lpPrG1GRy5Mi",
        "outputId": "27d8497b-6753-4fdc-ea0a-d4afe84573c5"
      },
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "-1"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.find(\"ss\", 3)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gzzjIQfUy6wc",
        "outputId": "1d737c17-0673-4117-9a8e-a9f340fa6e1c"
      },
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "5"
            ]
          },
          "metadata": {},
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.find(\"ss\", 0, 3)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BhgN6cjZzPY4",
        "outputId": "f9957858-3fc6-4936-e02a-38b11b827b1e"
      },
      "execution_count": 50,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "-1"
            ]
          },
          "metadata": {},
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.rfind(\"ss\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IQon3i0izQ1q",
        "outputId": "f241eacc-fb06-49e1-cb72-22bd04ab132b"
      },
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "5"
            ]
          },
          "metadata": {},
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.count(\"ss\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_-hU9i2jzzmX",
        "outputId": "2bdb01cd-c48d-45a7-a9e4-a1b3af78de39"
      },
      "execution_count": 52,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {},
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.startswith(\"Miss\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b4uPi1_wz_CY",
        "outputId": "2c272800-f7d0-4676-f44c-4c105b5882ad"
      },
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.startswith(\"Mist\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Sv06PQUk0MIe",
        "outputId": "238e35a1-a524-4a5c-d828-8a7c03e1ac3c"
      },
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 54
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.endswith(\"pi\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wj3iWLwo0NqQ",
        "outputId": "a9bbdc5c-ca1d-49d0-84e6-ed32dbe95f12"
      },
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 55
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.endswith(\"p\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AG9Tm8330QBL",
        "outputId": "3ebc65cc-6764-462b-a328-f186b2d8e268"
      },
      "execution_count": 56,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 56
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.endswith((\"i\", \"u\"))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CL7073-20Rw4",
        "outputId": "6bf040c7-709f-44d1-9dad-a8083d874741"
      },
      "execution_count": 57,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 57
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.4.5 Modifying strings"
      ],
      "metadata": {
        "id": "M0MV1w8mePQT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"Mississippi\"\n",
        "x.replace(\"ss\", \"+++\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "fIP4byCk0bHQ",
        "outputId": "47868a03-da88-494c-8531-a23f7afcf7a1"
      },
      "execution_count": 58,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Mi+++i+++ippi'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 58
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"~x ^ (y % z)\"\n",
        "table = x.maketrans(\"~^()\", \"!&[]\")\n",
        "x.translate(table)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "Uoz0l1rw0jqs",
        "outputId": "9d62eeb9-8bec-465c-dc24-65955648131b"
      },
      "execution_count": 59,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'!x & [y % z]'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 59
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"~x ^ (y % z)\"\n",
        "table = x.maketrans(\"\", \"\", \"()~^%\")\n",
        "x.translate(table)"
      ],
      "metadata": {
        "id": "lHokVlrhX6HH",
        "outputId": "449be7d9-3193-4f1e-fdab-c43fa250e677",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 60,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'x  y  z'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 60
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: String searching\n",
        "If you want to see whether a line ends with the string \"rejected\", what string method would you use? Are there any other ways you could get the same result?\n"
      ],
      "metadata": {
        "id": "GDp9QhO4hEiR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.4.6 Modifying strings with list manipulations"
      ],
      "metadata": {
        "id": "JvB1XjVieWQN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "text = \"Hello, World\"\n",
        "wordList = list(text)\n",
        "wordList[6:] = []\n",
        "wordList.reverse()\n",
        "text = \"\".join(wordList)\n",
        "print(text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ed8OBKBo0tkI",
        "outputId": "0e3d7e0e-1510-43ab-c12b-046895008f10"
      },
      "execution_count": 61,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            ",olleH\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Modifying strings\n",
        "What would be a quick way to change all punctuation in a string to spaces?\n"
      ],
      "metadata": {
        "id": "xXySVaMMhSpo"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "VSHB-dTEhWdj"
      },
      "execution_count": 61,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "punct = str.maketrans(\"!.,:;-?\", \"       \")\n",
        "x = \"This is text, with: punctuation! Right?\"\n",
        "x.translate(punct)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "7dbNneJthWiu",
        "outputId": "b25c48aa-63f5-418b-c47f-51910ce0f928"
      },
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'This is text  with  punctuation  Right '"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.4.7 Useful methods and constants"
      ],
      "metadata": {
        "id": "Q3kx0c7IedzM"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"123\"\n",
        "x.isdigit()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "j6VrAxS51AII",
        "outputId": "dc7e53c9-dda1-440d-a3c8-299422e69549"
      },
      "execution_count": 63,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 63
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.isalpha()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lZEbKG-k1N2Y",
        "outputId": "88a1f804-315e-4e9d-ca48-bc5959a4678a"
      },
      "execution_count": 64,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = \"M\"\n",
        "x.islower()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Wp8ud4JU1QLu",
        "outputId": "ab1eb40d-371d-43df-a9e3-16543db24b17"
      },
      "execution_count": 65,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 65
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.isupper()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "q5lfH9FP1SbK",
        "outputId": "bd378fc9-439f-4753-9ef6-bbcf53ca8305"
      },
      "execution_count": 66,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 66
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try This: String operations\n",
        "Suppose that you have a list of strings in which some (but not necessarily all) of the strings begin and end with the double quote character:\n",
        "```\n",
        "x = ['\"abc\"', 'def', '\"ghi\"', '\"klm\"', 'nop']\n",
        "```\n",
        "What code would you use on each element to remove just the double quotes?\n"
      ],
      "metadata": {
        "id": "E14GSPNBhhId"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "CCZ_v9XnhnPo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = ['\"abc\"', 'def', '\"ghi\"', '\"klm\"', 'nop']\n",
        "for item in x:\n",
        "    print(item.strip('\"'))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ofl6Dr8IhnYC",
        "outputId": "165b9191-8098-44f9-9c38-30c80780d2bc"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "abc\n",
            "def\n",
            "ghi\n",
            "klm\n",
            "nop\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.5 Converting from objects to strings"
      ],
      "metadata": {
        "id": "q9il5stjeov_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "repr([1, 2, 3])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "1BDoHHU_1USE",
        "outputId": "a3612982-778a-4301-ad1d-cba5c623c12c"
      },
      "execution_count": 67,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'[1, 2, 3]'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 67
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1]\n",
        "x.append(2)\n",
        "x.append([3, 4])\n",
        "'the list x is ' + repr(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "JSsTZ7Gp1syw",
        "outputId": "b6d3f012-2ecb-4c6b-e7bb-e393db0c227a"
      },
      "execution_count": 68,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'the list x is [1, 2, [3, 4]]'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 68
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "repr(len)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "N_EpP77d1vBE",
        "outputId": "0873f5c9-3a13-4ea1-af08-9aa6828d40d3"
      },
      "execution_count": 69,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'<built-in function len>'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 69
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.6 Using the format method\n",
        "\n",
        "## 6.6.1 The format method and positional parameters"
      ],
      "metadata": {
        "id": "jZdyV22HezQV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\"{0} is the {1} of {2}\".format(\"Ambrosia\", \"food\", \"the gods\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "CbaGZkAL12Ho",
        "outputId": "e834d021-c217-4e2c-925b-98f43641e3bb"
      },
      "execution_count": 70,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia is the food of the gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 70
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"{{Ambrosia}} is the {0} of {1}\".format(\"food\", \"the gods\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "ZutYddEJ2HBC",
        "outputId": "99b4d88f-6741-4df0-8812-1c952346744d"
      },
      "execution_count": 71,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'{Ambrosia} is the food of the gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 71
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.6.2 The format method and named parameters"
      ],
      "metadata": {
        "id": "P1SHLZu4iBk2"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\"{food} is the food of {user}\".format(food=\"Ambrosia\",\n",
        "      user=\"the gods\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "uJS2HsWZ2I5A",
        "outputId": "56022226-ea8a-4acf-87bf-2d92d806a191"
      },
      "execution_count": 72,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia is the food of the gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 72
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"{0} is the food of {user[1]}\".format(\"Ambrosia\",\n",
        "         user=[\"men\", \"the gods\", \"others\"])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "791KIMve2SVQ",
        "outputId": "13c61e39-1f7b-4292-ec3d-6247522032ef"
      },
      "execution_count": 73,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia is the food of the gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 73
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.6.3 Format specifiers"
      ],
      "metadata": {
        "id": "p8x77zuNiHNO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\"{0:10} is the food of gods\".format(\"Ambrosia\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "vjyHKxR22alt",
        "outputId": "5614d690-f02c-4714-fadc-aff207052f02"
      },
      "execution_count": 74,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia   is the food of gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 74
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"{0:{1}} is the food of gods\".format(\"Ambrosia\", 10)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "Jxc1xBut2v14",
        "outputId": "1372288e-bc60-46d0-ee61-3a65c0819650"
      },
      "execution_count": 75,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia   is the food of gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 75
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"{food:{width}} is the food of gods\".format(food=\"Ambrosia\", width=10)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "z_MX_pTr2xkB",
        "outputId": "c63ec355-a1fa-4511-ede1-9d79c0e71a94"
      },
      "execution_count": 76,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia   is the food of gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 76
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"{0:>10} is the food of gods\".format(\"Ambrosia\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "CV2AxtKq2zRQ",
        "outputId": "4fc9257d-9bd2-417b-d382-1ea074c2654e"
      },
      "execution_count": 77,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'  Ambrosia is the food of gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 77
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"{0:&>10} is the food of gods\".format(\"Ambrosia\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "LGaEWsun209P",
        "outputId": "226b432b-4e6c-4a85-f954-131f63ebda8a"
      },
      "execution_count": 78,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'&&Ambrosia is the food of gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 78
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: the format() method\n",
        "What will be in x when the following snippets of code are executed?:\n",
        "```\n",
        "x = \"{1:{0}}\".format(3, 4)\n",
        "x = \"{0:$>5}\".format(3)\n",
        "x = \"{a:{b}}\".format(a=1, b=5)\n",
        "x = \"{a:{b}}:{0:$>5}\".format(3, 4, a=1, b=5, c=10)\n",
        "```"
      ],
      "metadata": {
        "id": "TZ0u9M_niOGd"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# String interpolation with f-strings"
      ],
      "metadata": {
        "id": "JhiVJyEcfCUo"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "value = 42\n",
        "message = f\"The answer is {value}\"\n",
        "print(message)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CEbs0bJ-5KBt",
        "outputId": "7beb0305-854a-45ec-b8c4-40992ba8e995"
      },
      "execution_count": 79,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The answer is 42\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "primes = [1, 2, 3, 5]\n",
        "f\"sum of first 4 primes is {sum(primes)}\""
      ],
      "metadata": {
        "id": "uW_zV1okkQEF",
        "outputId": "69a568f3-12d5-4458-dd15-4e1c126533a5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 80,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'sum of first 4 primes is 11'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 80
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "PI = 3.1415\n",
        "print(f\"PI is {PI:{10}.{2}}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-moP9HpC5dzt",
        "outputId": "467ab19c-ea3b-47db-b0bf-382b2a6e4615"
      },
      "execution_count": 81,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "PI is        3.1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(f\"{PI=:{10}.{2}}\")"
      ],
      "metadata": {
        "id": "X6dAnMywkGiG",
        "outputId": "96e72d18-c6e5-4359-de94-0792641d37a9",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 82,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "PI=       3.1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "primes = [1, 2, 3, 5]\n",
        "f\"sum of first 4 primes is {sum(primes)=}\""
      ],
      "metadata": {
        "id": "__uTDbQGkgSf",
        "outputId": "26534d20-2289-4011-b959-963376912e94",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 83,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'sum of first 4 primes is sum(primes)=11'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 83
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.8 Formatting strings with %"
      ],
      "metadata": {
        "id": "orYq71OkfF5v"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\"%s is the %s of %s\" % (\"Ambrosia\", \"food\", \"the gods\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "auaFXaPP22-i",
        "outputId": "833956da-9222-4245-972d-6fffea17290d"
      },
      "execution_count": 84,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Ambrosia is the food of the gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 84
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"%s is the %s of %s\" % (\"Nectar\", \"drink\", \"gods\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "DtKOxY3i3WSM",
        "outputId": "200297bf-75e7-4681-d044-0a3e955a65dd"
      },
      "execution_count": 85,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Nectar is the drink of gods'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 85
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"%s is the %s of the %s\" % (\"Brussels Sprouts\", \"food\",\n",
        "       \"foolish\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "H8B6JZJz3hDo",
        "outputId": "329e21dc-da44-4d40-c1f6-655a45a33bb6"
      },
      "execution_count": 86,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Brussels Sprouts is the food of the foolish'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 86
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, \"three\"]\n",
        "\"The %s contains: %s\" % (\"list\", x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "oohWJhwz3igA",
        "outputId": "58dd0710-224e-4fd4-9b1f-a2f95deb6a47"
      },
      "execution_count": 87,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"The list contains: [1, 2, 'three']\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 87
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"Pi is <%-6.2f>\" % 3.14159 # use of the formatting sequence: %–6.2f"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "ZRGHKw6l3va8",
        "outputId": "d7a76ff2-7ca2-4593-fcd3-2a68d57ec6ff"
      },
      "execution_count": 88,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Pi is <3.14  >'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 88
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 6.8.2 Named parameters and formatting sequences"
      ],
      "metadata": {
        "id": "4xa4ppAdibq-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\"%(PI).2f\""
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "szm3pA4M37xO",
        "outputId": "4261adb2-e055-47d7-8494-8e19f41e705c"
      },
      "execution_count": 89,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'%(PI).2f'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 89
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "num_dict = {'e': 2.718, 'PI': 3.14159}\n",
        "print(\"%(PI).2f - %(PI).4f - %(e).2f\" % num_dict)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yNPTayqF4L6l",
        "outputId": "876e510d-122c-4773-8bcd-70696522be80"
      },
      "execution_count": 90,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "3.14 - 3.1416 - 2.72\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"a\")\n",
        "print(\"b\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tyWR0z4d4WoW",
        "outputId": "5079f7d4-95ff-435c-82f7-5015140233f1"
      },
      "execution_count": 91,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "a\n",
            "b\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"a\", \"b\", \"c\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VXaLgiVK4_5W",
        "outputId": "619c0626-d3bc-4287-db3c-16afb0157fef"
      },
      "execution_count": 92,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "a b c\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"a\", \"b\", \"c\", sep=\"|\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RWe8puoD5Fa7",
        "outputId": "11b58e62-c55d-491a-cab4-b92d2627c29f"
      },
      "execution_count": 93,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "a|b|c\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"a\", \"b\", \"c\", end=\"\\n\\n\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EEUpy-ik5HsU",
        "outputId": "dd0a1441-c46d-4950-822c-54a858bc12a8"
      },
      "execution_count": 94,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "a b c\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Formatting strings with %\n",
        "What would be in the variable x after the following snippets of code have executed?\n",
        "```\n",
        "x = \"%.2f\" % 1.1111\n",
        "x = \"%(a).2f\" % {'a':1.1111}\n",
        "x = \"%(a).08f\" % {'a':1.1111}\n",
        "```"
      ],
      "metadata": {
        "id": "asGDM3A7ih_-"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 6.9 Bytes"
      ],
      "metadata": {
        "id": "IidyabC6fPIU"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "unicode_a_with_acute = '\\N{LATIN SMALL LETTER A WITH ACUTE}'\n",
        "unicode_a_with_acute"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "6Byp2N9q5m_y",
        "outputId": "4136693b-1a81-45aa-ec37-64d7b7c7b4f7"
      },
      "execution_count": 95,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'á'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 95
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "xb = unicode_a_with_acute.encode()   #A\n",
        "xb"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CY8GW7FO513R",
        "outputId": "f0e0a1a9-8fda-4168-fad9-ab09949a23bf"
      },
      "execution_count": 96,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "b'\\xc3\\xa1'"
            ]
          },
          "metadata": {},
          "execution_count": 96
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "xb += 'A'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "BrmDj8jF54yM",
        "outputId": "1b9eea62-abf3-4735-c229-b2b46665e35a"
      },
      "execution_count": 97,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "can't concat str to bytes",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-97-2c4ecee004ef>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mxb\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;34m'A'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: can't concat str to bytes"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "xb.decode()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "p2j1wWik57VX",
        "outputId": "e743bc4d-f4bf-46ce-fbca-e6cfde6027c1"
      },
      "execution_count": 98,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'á'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 98
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Bytes\n",
        "For which of the following kinds of data would you want to use a string? For which could you use bytes?\n",
        "1. Data file storing binary data\n",
        "2. Text in a language with accented characters\n",
        "3. Text with only uppercase and lowercase roman characters\n",
        "4. A series of integers no larger than 255"
      ],
      "metadata": {
        "id": "1qHBqiddfVjo"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "1. Data file storing binary data\n",
        "\n",
        "   Bytes. Because the data is binary, you're more concerned with the contents as numbers rather than text. Therefore, it would make sense to use bytes.\n",
        "2. Text in a language with accented characters\n",
        "\n",
        "   String. Python 3 strings are Unicode, so they can handle accented characters.\n",
        "3. Text with only uppercase and lowercase roman characters\n",
        "\n",
        "   String. Strings should be used for all text in Python 3.\n",
        "4. A series of integers no larger than 255\n",
        "\n",
        "   Bytes. A byte is an integer no larger than 255, so the bytes type is perfect for storing integers like this.\n"
      ],
      "metadata": {
        "id": "Bd7uqFnbizvy"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Lab 6: Preprocessing Text\n",
        "In processing raw text, it's quite often necessary to clean and normalize the text before doing anything else. If you want to find the frequency of words in text, for example, you can make the job easier if, before you start counting, you make sure that everything is lowercase (or uppercase, if you prefer) and that all punctuation has been removed. It can also make things easier to break the text into a series of words.\n",
        "\n",
        "In this lab, the task is to read an excerpt of the first chapter of Moby Dick, make sure that everything is one case, remove all punctuation, and write the words one per line to a second file. Again, because I haven’t yet covered reading and writing files, the code for those operations is supplied below.\n",
        "Your task is to come up with the code to replace the commented lines in the sample below:\n",
        "```\n",
        "with open(\"moby_01.txt\") as infile, open(\"moby_01_clean.txt\", \"w\") as outfile:\n",
        "    for line in infile:\n",
        "        # make all one case\n",
        "        # remove punctuation\n",
        "        # split into words\n",
        "        # write all words for line\n",
        "        outfile.write(cleaned_words)\n",
        "```"
      ],
      "metadata": {
        "id": "G-xwdt8IC8oJ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Run the cell below to get the file moby_01.txt"
      ],
      "metadata": {
        "id": "TvMFvsM2DKgf"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!wget https://raw.githubusercontent.com/nceder/qpb4e/main/code/Chapter%2006/moby_01.txt &> null  && echo Downloaded\n",
        ""
      ],
      "metadata": {
        "id": "THiA8sWMyOYo",
        "outputId": "afb978c5-99fa-4a58-d123-a4b9a60d0e25",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 99,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Downloaded\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Create your solution in this cell\n",
        "\n",
        "with open(\"moby_01.txt\") as infile, open(\"moby_01_clean.txt\", \"w\") as outfile:\n",
        "    for line in infile:\n",
        "        # make all one case\n",
        "        # remove punctuation\n",
        "        # split into words\n",
        "        # write all words for line\n",
        "        outfile.write(cleaned_words)\n"
      ],
      "metadata": {
        "id": "8eetSxZTDahh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Solution created by the author\n",
        "import string\n",
        "\n",
        "punct = str.maketrans(\"\",  \"\", string.punctuation)\n",
        "\n",
        "with open(\"moby_01.txt\") as infile:\n",
        "  with open(\"moby_01_clean.txt\", \"w\") as outfile:\n",
        "    for line in infile:\n",
        "        # make all one case\n",
        "        cleaned_line = line.lower()\n",
        "\n",
        "        # remove punctuation\n",
        "        cleaned_line = cleaned_line.translate(punct)\n",
        "\n",
        "        # split into words\n",
        "        words = cleaned_line.split()\n",
        "        cleaned_words = \"\\n\".join(words) + \"\\n\"\n",
        "        # write all words for line\n",
        "        outfile.write(cleaned_words)\n"
      ],
      "metadata": {
        "id": "J97n4P02yKvW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# prompt: For every line the file moby_01.txt, make everything lower case,\n",
        "#         remove punctuation, and split into words,\n",
        "#         and write the words one per line  to the file moby_01_clean.txt\n",
        "\n",
        "import string\n",
        "\n",
        "# Open the input and output files\n",
        "with open('moby_01.txt', 'r') as f_in, open('moby_01_clean.txt', 'w') as f_out:\n",
        "  for line in f_in:\n",
        "    # Convert the line to lowercase\n",
        "    line = line.lower()\n",
        "\n",
        "    # Remove punctuation\n",
        "    for char in string.punctuation:\n",
        "      line = line.replace(char, '')\n",
        "\n",
        "    # Split the line into words\n",
        "    words = line.split()\n",
        "\n",
        "    # Write the words to the output file\n",
        "    f_out.write(' '.join(words) + '\\n')\n"
      ],
      "metadata": {
        "id": "ZxKvX04N6FGe"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated usinb CoPilot\n",
        "# Prompt: or every line the file moby_01.txt, make everything lower case,\n",
        "#         remove punctuation, and split into words.\n",
        "#         Write the words one per line to the file moby_01_clean.txt.\n",
        "\n",
        "import string\n",
        "\n",
        "# Open the input file for reading\n",
        "with open('moby_01.txt', 'r') as input_file:\n",
        "    # Open the output file for writing\n",
        "    with open('moby_01_clean.txt', 'w') as output_file:\n",
        "        # Iterate over each line in the input file\n",
        "        for line in input_file:\n",
        "            # Convert the line to lowercase\n",
        "            line = line.lower()\n",
        "\n",
        "            # Remove punctuation from the line\n",
        "            line = line.translate(str.maketrans('', '', string.punctuation))\n",
        "\n",
        "            # Split the line into words\n",
        "            words = line.split()\n",
        "\n",
        "            # Write each word to the output file\n",
        "            for word in words:\n",
        "                output_file.write(word + '\\n')"
      ],
      "metadata": {
        "id": "PEoGSG9Ize34"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}